package com.sample;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.DecisionTableConfiguration;
import org.drools.builder.DecisionTableInputType;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;

/**
 * This is a sample class to launch a decision table.
 */
public class DecisionTableTest {

    public static final void main(String[] args) {
        try {
            // load up the knowledge base
            KnowledgeBase kbase = readKnowledgeBase();
            StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
            KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
            // go !
             
            User user = new User();
            user.setUserType("INT");
            user.setPreferredCountry("US");
            
            
            //Add Roles to user
            List<String> roles = new ArrayList<String>();
            //roles.add("GEFLTTELEMATICSEXT");
           // roles.add("GEFLTTELEBETAUSER");
            roles.add("GEFLTTELEBETATESTING");
            roles.add("GEFLTFIELDSERVICES1");
            user.setRoles(roles);
          
	        FactsPayload factsPayload = new FactsPayload();	            
            factsPayload.setUser(user);
            factsPayload.setEnvironment("EXTERNAL_SITE");
            factsPayload.setTier("QA");	           
            ksession.insert(factsPayload);            
            
            ksession.fireAllRules();

            System.out.println("================================");
            System.out.println();
            for(String url : factsPayload.getUrlsWithAccess()){
            	System.out.println(url);
            }
            
            logger.close();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    private static KnowledgeBase readKnowledgeBase() throws Exception {
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        DecisionTableConfiguration config = KnowledgeBuilderFactory.newDecisionTableConfiguration();
        config.setInputType(DecisionTableInputType.XLS);
        kbuilder.add(ResourceFactory.newClassPathResource("Sample.xls"), ResourceType.DTABLE, config);
        KnowledgeBuilderErrors errors = kbuilder.getErrors();
        if (errors.size() > 0) {
            for (KnowledgeBuilderError error: errors) {
                System.err.println(error);
            }
            throw new IllegalArgumentException("Could not parse knowledge.");
        }
        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
        kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
        return kbase;
    }

    
    
    public static class User{
    	private String userType; //INT OR EXT
    	private List<String> roles;
    	private String preferredCountry;
    	
		public String getUserType() {
			return userType;
		}
		public void setUserType(String userType) {
			this.userType = userType;
		}
		public List<String> getRoles() {
			return roles;
		}
		public void setRoles(List<String> roles) {
			this.roles = roles;
		}
		public String getPreferredCountry() {
			return preferredCountry;
		}
		public void setPreferredCountry(String preferredCountry) {
			this.preferredCountry = preferredCountry;
		}    	
		
    }

    
    
    public static class FactsPayload{
    	private User user;
    	private String environment;  //'INT_SITE' or 'EXT_SITE'
    	private String tier; // 'TEST', 'QA', 'PROD'
    	private String roleToCheckFor;
    	private LinkedHashSet<String> urlsWithAccess = new LinkedHashSet<String>();
    	
		public User getUser() {
			return user;
		}
		public void setUser(User user) {
			this.user = user;
		}
		public String getEnvironment() {
			return environment;
		}
		public void setEnvironment(String environment) {
			this.environment = environment;
		}
		public String getTier() {
			return tier;
		}
		public void setTier(String tier) {
			this.tier = tier;
		}
		public String getRoleToCheckFor() {
			return roleToCheckFor;
		}
		public void setRoleToCheckFor(String roleToCheckFor) {
			this.roleToCheckFor = roleToCheckFor;
		}
		public LinkedHashSet<String> getUrlsWithAccess() {
			return urlsWithAccess;
		}
		public void setUrlsWithAccess(LinkedHashSet<String> urlsWithAccess) {
			this.urlsWithAccess = urlsWithAccess;
		}
			
    	
    	
    }
    
}
